﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Editor.PageControls;
using System.Windows.Forms;
using System.Xml.Serialization;
using Editor.Utilities;
using System.Collections;

namespace Editor.QuestionInterface
{
    public class InteractivePage : PageControl
    {
        public string Answers { get; set; }
        public string MainQuestionText { get; set; }
        public string Subject { get; set; }
        public string Result { get; set; }
        public string Format { get; set; }
        public string CorrectAnswerDescription { get; set; }
        public string IncorrectAnswerDescription { get; set; }

        [XmlIgnore]
        public List<XmlAnything<HelperQuestion>> HelperQuestions { get; set; }

        public List<XmlAnything<HelperQuestion>> SHelperQuestions
        {
            get
            {
                if (HelperQuestions.Count == 0)
                {
                    return HelperQuestions;
                }

                var hqs = new List<XmlAnything<HelperQuestion>>();
                foreach (var q in HelperQuestions)
                {
                    if (q.Value is AlfanumBasedHelperQuestion)
                    {
                        CAlfanumQuestion cq = new CAlfanumQuestion();
                        cq.Answer = q.Value.Answer;
                        cq.BodyText = q.Value.BodyText;
                        cq.CorrectAnswerDescription = q.Value.CorrectAnswerDescription;
                        cq.IncorrectAnswerDescription = q.Value.IncorrectAnswerDescription;
                        hqs.Add(new XmlAnything<HelperQuestion>(cq));
                    }
                    else if (q.Value is ChoiceBasedHelperQuestion)
                    {
                        CChoiceQuestion cq = new CChoiceQuestion();
                        cq.Answer = q.Value.Answer;
                        cq.BodyText = q.Value.BodyText;
                        cq.CorrectAnswerDescription = q.Value.CorrectAnswerDescription;
                        cq.IncorrectAnswerDescription = q.Value.IncorrectAnswerDescription;
                        hqs.Add(new XmlAnything<HelperQuestion>(cq));
                    }
                }
                return hqs;
            }
        }
        
        public InteractivePage()
        {
            HelperQuestions = new List<XmlAnything<HelperQuestion>>();
        }

        public override string NamingPrefix
        {
            get
            {
                return "qpage";
            }
        }

        protected override Control generateInnerControl()
        {
            QuestionPage questionPage = new QuestionPage();

            return questionPage;
        }

        protected override PropertiesFormBase generatePropertiesForm()
        {
            return null;
        }

        public override void UpdateInnerControl()
        {
            var questionPage = InnerControl as QuestionPage;

            questionPage.MainQuestionText = MainQuestionText;
            questionPage.Subject = Subject;
            questionPage.Format = Format;
            questionPage.Answers = Answers;
            questionPage.CorrectAnswerDescription = CorrectAnswerDescription;
            questionPage.IncorrectAnswerDescription = IncorrectAnswerDescription;
            
            var helperQs = new List<HelperQuestionUserControl>();

            HelperQuestion anq;
            HelperQuestionUserControl abhq;

            foreach (var question in HelperQuestions)
            {
                if (question.Value is AlfanumQuestion)
                {
                    anq = question.Value as AlfanumQuestion;
                    abhq = new AlfanumBasedHelperQuestion();

                }
                else if (question.Value is ChoiceQuestion)
                {
                    anq = question.Value as ChoiceQuestion;
                    abhq = new ChoiceBasedHelperQuestion();
                }
                else
                {
                    anq = abhq = null;
                }

                abhq.Answer = anq.Answer;
                abhq.BodyText = anq.BodyText;
                abhq.CorrectAnswerDescription = anq.CorrectAnswerDescription;
                abhq.IncorrectAnswerDescription = anq.IncorrectAnswerDescription;
                abhq.QuestionParent = InnerControl as QuestionPage;
                helperQs.Add(abhq);
            }

            questionPage.HelperQuestions = helperQs;
        }

        public override object Clone()
        {
            var ip = new InteractivePage();

            ip.MainQuestionText = MainQuestionText;
            ip.Subject = Subject;
            ip.Format = Format;
            ip.Answers = Answers;
            ip.CorrectAnswerDescription = CorrectAnswerDescription;
            ip.IncorrectAnswerDescription = IncorrectAnswerDescription;

            return ip;
        }
    }
}
